﻿package com.ufenlei.LTS{
	import flash.display.Sprite;
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.events.Event;
	
	public class Block extends Sprite{
		protected var _mask:BlockMask;
		protected var _container:Sprite;
		private var _bWidth:uint;
		private var _bHeight:uint;
		private var _scrollWidth:uint;
		private var _containtList:Array;
		private var _trueHeight:uint = 0;
		
		public function Block(bwidth:uint=100, bheight:uint=100){
			_bWidth = bwidth;
			_bHeight = bheight;
			_scrollWidth = 10;
			_trueHeight = 0;
			_containtList = new Array();
			_container = new Sprite();
			setMask();
			drawFrame();
			_container.mask = this._mask;
			addChild(_container);
			_container.x = 1;
			_container.y =1;
			this.addEventListener(MouseEvent.CLICK, test);
		}
		public function test(evt:Event):void{
		}
		public function addContent(c:DisplayObject, blankHeight:int = 0):void{
			_container.addChild(c);
			_containtList.push(c);
			var t:uint = Math.max(c.getBounds(this).bottom, 0);
			c.y = _trueHeight + blankHeight;
			_trueHeight += Math.max((t + blankHeight), 0 );
			this.dispatchEvent( new Event(Event.CHANGE) );
		};
		public function get trueHeight():uint{
			return _trueHeight;
		}
		public function clear():void{
			while(_containtList.length > 0){
				_container.removeChild( _containtList.pop() );
			}
			this.dispatchEvent( new Event(Event.CHANGE) );
		}
		private function setMask():void{
			_mask = new BlockMask(_bWidth - _scrollWidth , _bHeight);
			this.addChild(_mask);
		}
		public function get bWidth():uint{
			return _bWidth;
		}
		public function get bHeight():uint{
			return _bHeight;
		}
		public function set bWidth(w:uint):void{
			this.setSize(w,-1);
		}
		public function set bHeight(h:uint):void{
			this.setSize(-1,h);
		}
		public function setSize(w:int=-1,h:int=-1):void{
			_bWidth = (w >= 0 )?w:_bWidth;
			_bHeight = (h>=0)?h:_bHeight;
			_mask.setSize(maskWidth , maskHeight);
			drawFrame();
			this.dispatchEvent( new Event(Event.CHANGE) );
		}
		private function get maskWidth():uint{
			return bWidth-_scrollWidth;
		}
		private function get maskHeight():uint{
			return bHeight-_scrollWidth;
		}
		private function drawFrame():void{
			this.graphics.clear();
			this.graphics.beginFill(0xffffff);
			this.graphics.lineStyle(1,0xffffff,0.8);
			this.graphics.lineTo(bWidth,0);
			this.graphics.lineTo(bWidth,bHeight);
			this.graphics.lineTo(0,bHeight);			
			this.graphics.lineTo(0,0);
			this.graphics.endFill();
		}
	}
}
import flash.display.Sprite;

class BlockMask extends Sprite{
	private var _bWidth:uint;
	private var _bHeight:uint;
	public function BlockMask(w:uint = 400, h:uint=300):void{
		_bWidth = w;
		_bHeight = h;
		setSize();
	}
	public function setSize(w:int = -1 , h:int=-1):void{		
		_bWidth = (w >= 0) ? w:_bWidth;
		_bHeight = (h >= 0) ? h:_bHeight;
		this.graphics.clear();
		this.graphics.beginFill(0x000000);
		this.graphics.drawRect(0,0,_bWidth,_bHeight);
		this.graphics.endFill();
	}
}
